<template>
  <div>
    <div class="header">
      <div class="right">
        <el-input v-model="input" placeholder="查找" class="myinput"></el-input>
        <el-button class="el-icon-search sou" @click="select"></el-button>
      </div>
      <el-button type="success" class="el-icon-circle-plus-outline" @click="add"
        >新增</el-button
      >
    </div>
    <el-table :data="nowData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="pid" label="父ID" width="180"> </el-table-column>
      <el-table-column prop="drug_class_name" label="名称"> </el-table-column>
      <el-table-column prop="sorting" label="排序"> </el-table-column>
      <el-table-column prop=" is_look" label="显示">
        <template slot-scope="scope">
          <el-tag
            type="info"
            class="el-icon-check"
            v-if="scope.row.is_look == '1'"
          ></el-tag>
          <el-tag
            type="success"
            class="el-icon-check"
            v-if="scope.row.is_look == '2'"
          ></el-tag></template
      ></el-table-column>
      <el-table-column label="操作" class="roeone" width="300">
        <template slot-scope="scope">
          <el-button
            type="primary"
            class="el-icon-edit modbt"
            @click="mod(scope.row)"
            >修改</el-button
          >
          <el-button
            type="danger"
            class="el-icon-close modbt"
            @click="del(scope.row.id)"
            >删除</el-button
          >
          <el-button
            type="success"
            class="el-icon-circle-plus-outline"
            @click="addp(scope.row.id)"
            >新增</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码内容 -->
    <div class="pages clear">
      <div class="leftPage">
        <span
          >共 <b>{{ countNum }}</b> 条 | 每页 <b>20</b> 条 | 共
          <b>{{ maxpage }}</b> 页</span
        >
      </div>
      <div class="rightPage">
        <el-pagination
          background
          layout="prev, pager, next"
          :page-count="maxpage"
          :current-page="nowpage"
          :countNum="countNum"
          @current-change="changePage"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { home, del } from "../../utils/api/classification";
export default {
  name: "",
  components: {},

  data() {
    return {
      tableData: [],
      input: "",
      maxpage: 0,
      nowpage: 1,
      countNum: 0,
      all: {
        // index: " 1",
        // pageSize: null,
        search: null,
      },
    };
  },
  created() {
    this.home();
  },
  watch: {},
  methods: {
    //搜索
    select() {
      this.all.search = this.input;
      this.home();
      console.log(this.all.search);
    },
    //初始化
    home() {
      home(this.all).then((data) => {
        this.tableData = [];
        this.dataChuli(data.data.data.drugClsVoOne);
        this.countNum = data.data.data.allCount;
        this.maxpage = Math.ceil(this.countNum / 20);
      });
    },
    //初始化处理函数
    dataChuli(data) {
      for (let i = 0; i < data.length; i++) {
        this.tableData.push({
          id: data[i].id,
          pid: data[i].pid,
          drug_class_name: data[i].drug_class_name,
          is_look: data[i].is_look,
          sorting: data[i].sorting,
          note: data[i].note,
          type: data[i].type,
        });
        if (data[i].children != undefined) {
          this.dataChuli(data[i].children);
        }
      }
    },
    // 当前页改变时触发的函数，例如：当点击第二页时，当前页的页码变为2
    changePage(num) {
      if (this.nowpage > this.maxpage) {
        this.nowpage = this.maxpage;
      } else {
        this.nowpage = num;
      }
    },

    add() {
      this.$router.push("/home/classification/classification_add");
    },
    addp(id) {
      this.$router.push({
        path: "/home/classification/classification_addp",
        query: { id },
      });
    },
    mod(row) {
      this.$router.push({
        path: "/home/classification/classification_mod",
        query: { ...row },
      });
    },
    del(id) {
      del(id).then((data) => {
        this.home();
        console.log(data);
      });
      this.$message({
        message: "您已成功删除",
        type: "success",
      });
    },
  },
  computed: {
    nowData() {
      let qiShi = (parseInt(this.nowpage) - 1) * 20;
      // console.log(this.nowpage);
      return this.tableData.slice(qiShi, qiShi + 20);
    },
  },
};
</script>
<style lang="less" scoped>
.top {
  padding: 10px;
  font-size: 18px;
  box-sizing: border-box;
}
.header {
  button {
    line-height: 5px;
    font-weight: bold;
    margin: 10px 6px;
  }
}
.left {
  float: left;
}
.right {
  float: right;
  button {
    height: 40px;
    margin: 0;
  }
  .myinput {
    width: 200px;
    height: 30px;
  }
}
.modbt {
  width: 70px;
  // height: 26px;
  line-height: 10px;
  font-weight: bold;
}
.pages {
  background-color: #fafafa;
  padding: 20px;
  .leftPage {
    float: left;
  }
  .rightPage {
    float: right;
  }
}
</style>